package cn.zhaoyuening.crm.service.impl;

import java.util.List;

import cn.zhaoyuening.crm.dao.StaffDao;
import cn.zhaoyuening.crm.domain.CrmStaff;
import cn.zhaoyuening.crm.service.StaffService;
import cn.zhaoyuening.crm.utils.MyStringUtils;

public class StaffServiceImpl implements StaffService {
	private StaffDao staffDao;
	
	public CrmStaff login(CrmStaff staff) {
		//将密码转换为md5加密
		if (staff.getLoginPwd()!=null) {
			String md5pw = MyStringUtils.getMd5String(staff.getLoginPwd());			
			staff.setLoginPwd(md5pw);
		} 
		return staffDao.login(staff);
	}

	public StaffDao getStaffDao() {
		return staffDao;
	}

	public void setStaffDao(StaffDao staffDao) {
		this.staffDao = staffDao;
	}

	public List<CrmStaff> findAll() {
		List<CrmStaff> list = staffDao.findAll();
		return list;
	}

	public CrmStaff getCrmStaffById(String id) {
		if (id==null) {
			return null;
		}
		CrmStaff staff = staffDao.findById(id);
		return staff;
	}

	@Override
	public void updateStaff(CrmStaff staff) {
		if (staff==null) {
			return;
		}
		//使用md5加密密码
		if (staff.getLoginPwd()!=null) {
			String md5PW = MyStringUtils.getMd5String(staff.getLoginPwd());
			staff.setLoginPwd(md5PW);
		}
		if (staff.getCrmPost()!=null&&staff.getCrmPost().getPostId().equals("")) {
			//取消职务分配
			staff.setCrmPost(null);
		}
		this.staffDao.addOrEdit(staff);
	}
	@Override
	public void addStaff(CrmStaff staff) {
		staffDao.save(staff);
	}
	@Override
	public void deleteStaff(CrmStaff staff) {
		staffDao.delete(staff);
	}
}
